home *** CD-ROM | disk | FTP | other *** search
- /*
- * MacDayd.c
- *
- * Macintosh Interface for the Daytime Client.
- *
- * Mike Trent 9/94
- *
- */
- /* Added Universal Header #ifdefs. Removed dead code.
- * MDT 11/27/95
- */
-
- // Determine if we are using universal headers..
- #ifdef UNIVERSAL_INTERFACES_VERSION
- #define __UNIVERSAL_HEADERS__
- #endif
-
-
- #include <MacTCPCommonTypes.h>
-
- #include <string.h>
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
-
- #include "ip.h"
- #include "iperr.h"
-
- /* defines */
- #define kBaseResID 128
- #define kMoveToFront (WindowPtr) - 1L
- #define kUseDefaultProc (void *)-1L
- #define kSleep 3600L
- #define kLeaveWhereItIs false
-
- #define mApple 128
- #define iAbout 1
-
- #define mFile 129
- #define iQuit 1
-
- #define mEdit 130
- /* dummy menu */
-
- #define mConnection 131
- #define iServing 1
- #define iLocalNum 2
-
- #define kIgnored 0
-
- void InitToolBox(void);
- void MenuBarInit(void);
- void DoEvent (EventRecord *eventPtr);
- void HandleMouseDown (EventRecord *eventPtr);
- void HandleMenuChoice (long menuChoice);
- void HandleConnection(short item);
- void DoAbout(void);
- void DoGetTime(char *host, char *port, char *data);
- void DrawDefault(DialogPtr dialog);
- OSErr DoMySpin(void);
- void DoServe(char *port, char *data);
-
- Boolean gDone = false;
-
- main()
- {
- EventRecord event;
-
- InitToolBox();
- MaxApplZone();
- MoreMasters();MoreMasters();MoreMasters();MoreMasters();
-
- if (InitMacTCP() != noErr) {
- SysBeep(3);
- ExitToShell();
- }
-
- MenuBarInit();
-
- gDone = false;
-
- while (gDone == false)
- {
- if (WaitNextEvent(everyEvent, &event, kSleep, nil))
- DoEvent (&event);
- else
- ;//HandleNull();
- }
- DisposeMacTCP();
- }
-
- void InitToolBox(void)
- {
- #ifdef __UNIVERSAL_HEADERS__
- InitGraf(&qd.thePort);
- #else
- InitGraf(&thePort);
- #endif // __UNIVERSAL_HEADERS__
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
- }
-
- void MenuBarInit (void)
- {
- Handle menuBar;
- MenuHandle menu;
-
- menuBar = GetNewMBar (kBaseResID);
- if (menuBar == nil)
- {
- SysBeep (3);
- ExitToShell();
- }
- SetMenuBar (menuBar);
- DisposHandle(menuBar);
-
- menu = GetMHandle(mApple);
- AddResMenu (menu, 'DRVR');
-
- DrawMenuBar();
- }
-
- void DoEvent (EventRecord *eventPtr)
- {
- char theChar;
-
- switch (eventPtr->what)
- {
- case mouseDown:
- HandleMouseDown (eventPtr);
- break;
- case keyDown:
- case autoKey:
- theChar = eventPtr->message & charCodeMask;
- if ((eventPtr->modifiers & cmdKey) != 0)
- HandleMenuChoice (MenuKey (theChar));
- break;
- }
- }
-
- /********** HandleMouseDown **********/
-
- void HandleMouseDown (EventRecord *eventPtr)
- {
- WindowPtr whichWindow;
- short thePart;
- long menuChoice;
-
- thePart = FindWindow(eventPtr->where, &whichWindow);
-
- switch (thePart)
- {
- case inMenuBar:
- menuChoice = MenuSelect (eventPtr->where);
- HandleMenuChoice(menuChoice);
- break;
- case inSysWindow:
- SystemClick(eventPtr, whichWindow);
- break;
- case inDrag:
- #ifdef __UNIVERSAL_HEADERS__
- DragWindow(whichWindow, eventPtr->where, &qd.screenBits.bounds);
- #else
- DragWindow(whichWindow, eventPtr->where, &screenBits.bounds);
- #endif // __UNIVERSAL_HEADERS__
- break;
- }
- }
-
-
- /********** HandleMenuChoice **********/
-
- void HandleMenuChoice (long menuChoice)
- {
- short menu;
- short item;
- MenuHandle appleMenu;
- Str255 accName;
- short accNumber;
-
-
- if (menuChoice != 0)
- {
- menu = HiWord(menuChoice);
- item = LoWord(menuChoice);
-
- switch (menu){
- case mApple:
- switch (item){
- case iAbout:
- DoAbout();
- break;
- default:
- appleMenu = GetMHandle (mApple);
- GetItem (appleMenu, item, accName);
- accNumber = OpenDeskAcc(accName);
- break;
- }
- break;
- case mFile:
- if (item == iQuit) gDone = true;
- break;
- case mConnection:
- HandleConnection(item);
- break;
- }
- HiliteMenu (0);
- }
- }
-
- void HandleConnection(short item)
- {
- char data[80], name[80], addr[16], port[10];
- DialogPtr dialog;
- short itemHit = 0;
- short t;
- Handle h;
- Rect r;
-
- switch (item) {
- case iServing:
- if ((GetHostName(&(name[1]), 0)) < 0) {
- SysBeep(3);
- return;
- }
- name[0] = (char)strlen(&(name[1])); // Make it a pascal string
-
- /* ask which port # to serve on (13 default?) */
- if ((dialog = GetNewDialog(128, nil, kMoveToFront)) == nil){
- SysBeep(3);
- return;
- }
- SetPort(dialog);
- GetDItem(dialog,3,&t,&h,&r);
- SetIText(h, (ConstStr255Param)name); /* identify host's name/number */
- DrawDefault(dialog);
-
- while ((itemHit != 1) && (itemHit != 2))
- ModalDialog(nil, &itemHit);
- if (itemHit == 2) {
- DisposDialog(dialog);
- return;
- }
- itemHit = 0;
-
- GetDItem(dialog,4,&t,&h,&r);
- GetIText(h, (unsigned char *)port);
-
- DisposDialog(dialog);
-
- port[(port[0]+1)] = 0; //C Terminate...
- DoServe(&(port[1]), data);
-
- /* tell user a connection was made */
- if ((dialog = GetNewDialog(129,nil, kMoveToFront)) == nil){
- SysBeep(3);
- return;
- }
- SetPort(dialog);
- GetDItem(dialog,2,&t,&h,&r);
- SetIText(h, (ConstStr255Param)data);
- DrawDefault(dialog);
- while (itemHit != 1)
- ModalDialog(nil, &itemHit);
- DisposDialog(dialog);
- break;
- case iLocalNum:
- if ((GetHostNameOnly(&(name[1]))) < 0) {
- strcpy(name, (const char *)"\pNo DNS Name");
- } else name[0] = (char)strlen(&(name[1])); // Make it a pascal string
- if ((GetMyIPDot(&(addr[1]))) < 0) {
- SysBeep(3);
- return;
- }
- addr[0] = (char)strlen(&(addr[1]));
-
- if ((dialog = GetNewDialog(130,nil, kMoveToFront)) == nil){
- SysBeep(3);
- return;
- }
- SetPort(dialog);
- GetDItem(dialog,2,&t,&h,&r);
- SetIText(h, (ConstStr255Param)addr);
- GetDItem(dialog,3,&t,&h,&r);
- SetIText(h, (ConstStr255Param)name);
-
- DrawDefault(dialog);
- while (itemHit != 1)
- ModalDialog(nil, &itemHit);
- DisposDialog(dialog);
- break;
- }
- }
-
- void DoServe(char *port, char *data)
- {
- DialogPtr d;
- struct sockaddr_in sa;
- int s, m;
- char *pts;
- time_t now;
- Boolean serving = true;
- short t;
- Handle h;
- Rect r;
- char addr[80];
-
- m = socket(kIgnored, kIgnored,IPPROTO_TCP);
- if (m < 0) {
- sprintf(data, (const char *) "\psocket error");
- return;
- }
- //sa.sin_family = AF_INET
- //sa.sin_addr.s_addr = INADDR_ANY
- sa.sin_port = atoi(port);
-
- if (bind (m,&sa, kIgnored)) {
- sprintf(data, (const char *) "\pbind error");
- return;
- }
-
- d = GetNewDialog(132, nil, (WindowPtr)-1L);
- if (!d) {
- sprintf(data, (const char *) "\pDLOG resource error");
- return;
- }
- DrawDefault(d);
- SetSpin(DoMySpin);
-
- while (1) { /* jury rigged infinite loop */
- if ((s = accept(m, &sa,kIgnored)) == -1) {
- sprintf(data, (const char *)"\paccept error");
- DisposeDialog(d);
- return;
- }
-
- (void) time(&now);
- pts = ctime(&now);
- (void) write(s, pts, strlen(pts));
- strcpy(&(data[1]), pts); data[0] = (strlen(&(data[1]) -1));
-
- (void) close(s);
-
- num2dot(sa.sin_addr, (char *)&addr[1]);
- addr[0] = strlen((char*)&addr[1]);
- sprintf(port, (const char *)"\p%d", sa.sin_port);
-
- GetDItem(d,2,&t,&h,&r);
- SetIText(h, (ConstStr255Param)addr);
- GetDItem(d,3,&t,&h,&r);
- SetIText(h, (ConstStr255Param)port);
- GetDItem(d,4,&t,&h,&r);
- SetIText(h, (ConstStr255Param)data);
- }
- return;
- }
-
-
-
- void DoAbout(void)
- {
- short itemHit = 0;
- DialogPtr dialog;
-
- dialog = GetNewDialog(131, nil, kMoveToFront);
- if (dialog == 0L){
- SysBeep(3);
- ExitToShell();
- }
- SetPort(dialog);
- DrawDefault(dialog);
- while (itemHit != 1)
- ModalDialog(nil, &itemHit);
- DisposDialog(dialog);
-
- return;
- }
-
- /********** DrawDefault **********/
-
- void DrawDefault(DialogPtr dialog)
- {
- short type;
- Handle dummy;
- Rect button;
-
- SetPort(dialog);
- GetDItem(dialog,1,&type,&dummy,&button);
- PenSize(3,3);
- InsetRect(&button, -4,-4);
- FrameRoundRect(&button,16,16);
-
- return;
- }
-
- OSErr DoMySpin(void)
- {
- EventRecord event;
- DialogPtr d;
- short item, thePart;
- Boolean eventp;
- WindowPtr whichWindow;
-
- eventp = WaitNextEvent(everyEvent, &event, 30L, nil);
- if (IsDialogEvent(&event)) {
- if (DialogSelect(&event, &d, &item)) {
- if (item == 1) {
- DisposeMacTCP();
- ExitToShell();
- }
- }
- }
- if (eventp) {
- switch (event.what)
- {
- case mouseDown:
- thePart = FindWindow(event.where, &whichWindow);
-
- switch (thePart)
- {
- case inSysWindow:
- SystemClick(&event, whichWindow);
- break;
- case inDrag:
- #ifdef __UNIVERSAL_HEADERS__
- DragWindow(whichWindow, event.where, &qd.screenBits.bounds);
- #else
- DragWindow(whichWindow, event.where, &screenBits.bounds);
- #endif // __UNIVERSAL_HEADERS__
- break;
- }
- break;
- case keyDown:
- case autoKey:
- switch(event.message & charCodeMask) {
- case 0x0d: /* ret */
- case 0x03: /* enter */
- /* blink the button someday ... */
- DisposeMacTCP();
- ExitToShell();
- break;
- }
- break;
- case osEvt:
- if ((event.message & suspendResumeMessage) == resumeFlag) {
- /* resume */
- DrawDefault(FrontWindow());
- } else
- /* suspend */
- ;
- break;
- default: break;
- } /* switch */
- } else
- ;//HandleNull();
- return 0;
- }
-